package com.interauto.service;

import com.interauto.Dao.Mapper.CaseStInter;
import com.interauto.Dao.Mapper.CheckStInter;
import com.interauto.Dao.Mapper.InterStInter;
import com.interauto.Dao.entity.Caseinfo;
import com.interauto.Dao.entity.Checktable;
import com.interauto.Dao.entity.Interinfo;
import com.interauto.bean.AutoAssInfoBean;
import com.interauto.bean.AutoCaseBean;
import com.interauto.bean.AutoInterCaseBean;
import com.interauto.bean.autoenum.AutoExpectEnum;
import com.interauto.bean.autoenum.HttpEnum;
import com.interauto.bean.httpinfo.HttpRequestBean;
import com.interauto.util.HttpAutoUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 将数据库的对象处理成服务运行的
 */
@Component
public class AutoCaseBeanService {
    private static final Logger logger= LoggerFactory.getLogger(AutoCaseBeanService.class);
    @Autowired
    private CaseStInter caseStInter;
    @Autowired
    private CheckStInter checkStInter;
    @Autowired
    private InterStInter interStInter;


    /**
     * 通过用例ID，获取该用例的信息
     * @param caseid 用例ID
     * @return 用例对象
     */
    public AutoCaseBean getAutoCaseinfo(Integer caseid){
        if(caseid == null || caseid <1){
            return null;
        }
        try{
            Caseinfo caseinfo=caseStInter.getCaseinfo(caseid);
            if(caseinfo == null){
                logger.info("getCaseinfo is null;--->>>caseid:"+caseid);
                return null;
            }
            AutoCaseBean autoCaseBean=new AutoCaseBean(caseinfo.getId(),
                    caseinfo.getCasename(),
                    caseinfo.getCaseinfo());
            autoCaseBean.setAutoInterCaseBeans(getAutoInterCaseBean(caseid));
            return autoCaseBean;
        }
        catch (Exception e){
            e.printStackTrace();
            logger.error("getAutoCaseinfo is system error --->>>"+e);
        }
        return null;
    }



    /**
     * 通过用例名，找到要运行的测试接口
     * @param caseid  用例ID
     * @return 运行的接口对象
     */
    public List<AutoInterCaseBean> getAutoInterCaseBean(int caseid){
        try{
            List<Interinfo> interinfoList= interStInter.getInterinfoforCaseid(caseid);
            if(interinfoList == null || interinfoList.isEmpty()){
                logger.info("--->>>>getInterinfoforCaseid is empty;caseid is :"+caseid);
                return null;
            }
            List<AutoInterCaseBean> autoInterCaseBeans=new ArrayList<>();
            for(Interinfo interinfo:interinfoList){
                AutoInterCaseBean autoInter=new AutoInterCaseBean();
                autoInter.setInterfacename(interinfo.getInterfacename());
                autoInter.setIntercasetype(interinfo.getIntercasetype());
                autoInter.setChecktype(interinfo.getChecktype());
                autoInter.setAutoAssInfoBeans(getAutoAssInfoBean(interinfo.getId())); //获取断言相关哈
                autoInter.setHttpRequestBean(getHttpRequestBean(interinfo));
                autoInterCaseBeans.add(autoInter);
            }
            return autoInterCaseBeans;
        }
        catch (Exception e){
            e.printStackTrace();
            logger.error("--->>>getAutoInterCaseBean is system error:"+e);
        }
        return null;
    }


    /**
     * 将数据库里存的接口信息，转换成运行的http对象
     * @param interinfo 数据库里存的接口信息
     * @return 要运行的http对象
     */
    public HttpRequestBean getHttpRequestBean(Interinfo interinfo){
        if(interinfo == null){
            return null;
        }
        HttpRequestBean httpRequestBean=new HttpRequestBean();
        httpRequestBean.setHttpparam(interinfo.getHttpparam());

        String url=interinfo.getHttpurl();
        HttpEnum.HttpType httpType=HttpEnum.HttpType.getHttpType(interinfo.getHttptype());
        if(!url.startsWith("http://") && !url.startsWith("https://")){  //判断数据库里的url是否有协议开头,如果没,就拼上.
            url=httpType!=null?(httpType.getHttptypename()+"://"+url):url;
        }
        httpRequestBean.setHttpurl(url);
        httpRequestBean.setPostparamtype(interinfo.getPostparamtype());
        httpRequestBean.setHttptype(httpType);
        httpRequestBean.setHttpmeth(HttpEnum.HttpMeth.getHttpMeth(interinfo.getHttpmeth()));
//        String headers=interinfo.getHeadstr();
//        if(StringUtils.isEmpty(headers)){
//            logger.info("-->>getHttpRequestBean header is empty。interinfo is:"+interinfo.toString());
//            return httpRequestBean;
//        }
//        Map<String,String> headermap= HttpAutoUtils.getHeaderMap(headers);
//
        Map<String,String> headermap=interinfo.getHeadstrmap();
        if(headermap == null || headermap.isEmpty()){
            String headers=interinfo.getHeadstr();
            if(StringUtils.isEmpty(headers)){
                logger.info("--->>>getHeaderMap is empty;");
                return httpRequestBean;
            }
            headermap= HttpAutoUtils.getHeaderMap(headers);
        }
        if(headermap.containsKey("cookie") || headermap.containsKey("Cookie")){
            if(headermap.containsKey("cookie")){
                httpRequestBean.setHttpcookie(HttpAutoUtils.getCookievaluetoMap(headermap.get("cookie")));
                headermap.remove("cookie");
            }
            if(headermap.containsKey("Cookie")){
                httpRequestBean.setHttpcookie(HttpAutoUtils.getCookievaluetoMap(headermap.get("cookie")));
                headermap.remove("Cookie");
            }
        }
        httpRequestBean.setHttpheader(headermap);
        return httpRequestBean;
    }


    /**
     * 从数据库查询断言相关，转换成运行的对象
     * @param interid 接口ID
     * @return 断言对象
     */
    public ArrayList<AutoAssInfoBean> getAutoAssInfoBean(int interid){
        try{
            List<Checktable> checktableList=checkStInter.getChecktableforinterid(interid);
            if(checktableList==null || checktableList.isEmpty()){
                logger.info("---->>>getChecktableforinterid is empty-->interid:"+interid);
                return null;
            }
            ArrayList<AutoAssInfoBean> autoAssInfoBeans=new ArrayList<>();
            for(Checktable checktable:checktableList){
                AutoAssInfoBean autoAssInfoBean=new AutoAssInfoBean();
                autoAssInfoBean.setCheckinfo(checktable.getCheckinfo());
                autoAssInfoBean.setExpectinfo(checktable.getExpectinfo());
                Integer autoExpectEnum=checktable.getAutoExpectEnum();
                autoAssInfoBean.setAutoExpectEnum(AutoExpectEnum.getAutoExpectEnum(autoExpectEnum));
                autoAssInfoBeans.add(autoAssInfoBean);
            }
            return autoAssInfoBeans;
        }
        catch (Exception e){
            e.printStackTrace();
            logger.error("--->>>getAutoAssInfoBean is system error:"+e);
            return null;
        }
    }
}
